var page = sm("do_Page");
var app=sm("do_App");
var nf = sm("do_Notification");
var config=require("config");
var cache = sm("do_DataCache");
var do_device=sm("do_Device");

var root=ui("$");  //$表示当前视图的根UI
var do_ALayout_root=ui("do_alayout_root");
var do_ListView_news=ui("do_ListView_news");
var listdataNews = mm("do_ListData");
var userinfo=cache.loadData("userinfo");
var pageNum=1;
var type_id=0;

do_ALayout_root.add("loadingUI", "source://view/loadingUI.ui", 0, 0);
var loadingUI = ui("loadingUI");
loadingUI.visible = true;	

root.setMapping({
	"do_alayout_root.tag":"id"
});

function refreshAllData(index,pages){	  
     pageNum=1;
	var http = mm("do_Http");
	http.method = "POST";  
	http.timeout = 30000; 
	http.contentType = "application/json"; 
	http.url = config.baseUrl+"getApproveList"; 
	http.body = JSON.stringify({userId:userinfo.userid,approveType:index,pageIndex:pageNum,pageSize:10,usernum:userinfo.usernum});
	http.on("success", function(data) {
		do_ListView_news.rebound();
		ui("do_note").visible=false;
		if(data.result==1 && data.data){
			if(data.data.length>0){
				data.data.forEach(function(item,i){
					item.approveType=type_id;
					item.callinmomey=parseFloat(item.callinmomey).toFixed(2);
				})
				listdataNews = mm("do_ListData");		
				listdataNews.addData(data.data);
			    do_ListView_news.bindItems(listdataNews);
			}
			else{				
			    ui("do_note").visible=true;
				//nf.toast(data.msg || "没有数据");
			}
		}
		else if(data.result==3045){	
			app.closePage({data:"帐号可能被其它终端登录，请重新登录", animationType:"", layer:pages});	
			 cache.saveData("userinfo", "");
		}
		else{			
			nf.toast(data.msg || "失败");
		}		
		loadingUI.visible = false;	
	});
	http.on("fail", function(data) {
		loadingUI.visible = false;	
		do_ListView_news.rebound();
		nf.toast(data.msg || "网络超时");
	});
	http.request();
}

function getNextPageData(index){
    pageNum++;
	var http = mm("do_Http");
	http.method = "POST"; 
	http.timeout = 30000; 
	http.contentType = "application/json"; 
	http.url = config.baseUrl+"getApproveList"; 
    http.body = JSON.stringify({userId:userinfo.userid,approveType:index,pageIndex:pageNum,pageSize:10,usernum:userinfo.usernum}); 
    http.on("success", function(data) {	
		do_ListView_news.rebound();
		if(data.result==1 && data.data){
			if(data.data.length>0){
				data.data.forEach(function(item,i){
					item.approveType=type_id;
				})
				listdataNews.addData(data.data);
			    do_ListView_news.bindItems(listdataNews);
			}
			else{
				pageNum--;
				nf.toast("没有更多数据了");
			}
		}
		else if(data.result==3045){	
			 app.closePage({data:"帐号可能被其它终端登录，请重新登录", animationType:"", layer:2});	
			 cache.saveData("userinfo", "");
		}
		else{
			nf.toast(data.msg || "失败");
		}	
	});
	http.on("fail", function(data) {
		do_ListView_news.rebound();
		nf.toast(data.msg || "网络超时");
	});
	http.request();
}

root.on("dataRefreshed", function(){
	type_id= do_ALayout_root.tag;
	//更新远程最新数据
	refreshAllData(type_id,1);
	
});

//下拉列表，刷新数据
do_ListView_news.on("pull", function(data){	
	if (data.state == 2){
		var nowdate=new Date();
		var olddate=nowdate.getTime();
		app.fire("tologin",{times:olddate,pagesize:2});
		listdataNews = mm("do_ListData");		
		listdataNews.removeAll();
		do_ListView_news.bindItems(listdataNews);
	    do_ListView_news.refreshItems();
		refreshAllData(type_id,2);
	}
});

//上拉列表，翻页数据
do_ListView_news.on("push", function(data){	
	if (data.state == 2){		
		var nowdate=new Date();
		var olddate=nowdate.getTime();
		app.fire("tologin",{times:olddate,pagesize:2});
		getNextPageData(type_id);
	}
});

page.on("result",function(result){
	var nowdate=new Date();
	var olddate=nowdate.getTime();
	app.fire("tologin",{times:olddate,pagesize:2});
	if(result && result.mark){			
		listdataNews = mm("do_ListData");		
		listdataNews.removeAll();
		do_ListView_news.bindItems(listdataNews);
	    do_ListView_news.refreshItems();
		refreshAllData(type_id,2);
		nf.toast(result.toast);
	}
});
app.on("updateList",function(){
	refreshAllData(0,2);
})